<?php
/**
 * 签到
 */
namespace module\index\controller;

use app\Controller;
use app\model\UserSignin as Model;

class Signin extends Controller
{
    /**
     * 签到首页
     * 
     */
    public function index()
    {
        $rows = Model::scope('user')->field(['id', 'date'])->order('date', 'DESC')->limit(7)->select()->toArray();
        $today = strtotime(date('Y-m-d 12:00:00'));
        $gap = 0;
        foreach($rows as $i => $row){
            if($row['date'] - $today <= 3600 * 24){
                $gap = $i;
                $today = $row['date'];
            } else {
                break;
            }
        }
        return json([
            'gap' => $gap
        ]);
    }
    /**
     * 提交
     * 
     */
    public function save()
    {
        $date = strtotime(date('Y-m-d 12:00:00'));
        $model = Model::where('date', $date)->scope('user')->findOrEmpty();
        $model->save(['date' => $date]);
        return json($model->toArray());
    }
}
